//
//  SHYViewController.m
//  SHYTool
//
//  Created by [sunhaiyan] on 06/12/2016.
//  Copyright (c) 2016 [sunhaiyan]. All rights reserved.
//

#import "SHYViewController.h"
#import "SHYCommonConfig.h"
#import "SHYValidJudge.h"
#import "NSDate+NSDate_Additional.h"

#import "CYTool.h"
#import "AFNetworking.h"
#import "ReactiveCocoa.h"
#import "UIButton+RACCommandSupport.h"

@interface SHYViewController ()

@property (nonatomic, strong) UIView *redView;
@property (nonatomic, strong) RACCommand *buttonCommand;

@end

@implementation SHYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton *testButton =  [UIButton buttonWithType:UIButtonTypeRoundedRect];
    testButton.frame =CGRectMake(10, 100, 80, 40);
    [testButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
    [testButton.layer setBorderWidth:1.0]; //边框宽度
    [testButton.layer setBorderColor:[[UIColor redColor] CGColor]];//边框颜色
    [testButton setTitle:@"点击" forState:UIControlStateNormal];
    [testButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    testButton.showsTouchWhenHighlighted = YES;
    
    testButton.rac_command = [[RACCommand alloc] initWithSignalBlock:^(id input) {
        return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [self buttonClick];
            [subscriber sendCompleted];
            return nil;
        }] timeout:0.3f onScheduler:[RACScheduler mainThreadScheduler]];
    }];
    
    [self.buttonCommand.executing subscribeNext:^(id x) {
        if ([x boolValue]) {
            NSLog(@"🌞👉%@👈🌞",[NSString stringWithFormat:@"%@", @"Yes"]);
        }
        else {
            NSLog(@"🌞👉%@👈🌞",[NSString stringWithFormat:@"%@", @"NO"]);
        }
    }];
//    self.buttonCommand.allowsConcurrentExecution = NO;
    
    testButton.rac_command = self.buttonCommand;
    self.redView = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
    self.redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.redView];
    
    [self.view addSubview:testButton];
}

- (RACSignal *)buttonClick {
    RACSignal *single = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber){
        NSLog(@"🌞👉%@👈🌞",[NSString stringWithFormat:@"%@", @"aaaaaaa"]);
        static BOOL isup = NO;
        isup = !isup;
        CGFloat changeTop = isup? 100 : -100;
        [UIView animateWithDuration:1 animations:^{
            self.redView.frame = CGRectMake(self.redView.frame.origin.x, self.redView.frame.origin.y + changeTop, self.redView.frame.size.width, self.redView.frame.size.height);
        } completion:^(BOOL finished) {
            [subscriber sendNext:@YES];
            [subscriber sendCompleted];
        }];
        return nil;
    }] timeout:1.0 onScheduler:[RACScheduler mainThreadScheduler]];
    return single;
}

- (void)asycmenthod:(void (^)(id responseDict, NSError *error))block {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [self get2:@"https://git.oschina.net/rssserver/CYTool" withParam:nil resultBlock:^(id responseDict, NSError *error) {
            block(responseDict,error);
        }];
        
    });
}

- (void)get2:(NSString *)url
  withParam:(NSDictionary *)paramDict
resultBlock:(void (^)(id responseDict, NSError *error))block {
    
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFHTTPResponseSerializer serializer];
    session.requestSerializer = [AFHTTPRequestSerializer serializer];
    [session GET:url  parameters:paramDict progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        block(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        block(nil, error);
    }];
}


@end
